package zdc.media {
import flash.display.Sprite;
import flash.events.AsyncErrorEvent;
import flash.events.Event;
import flash.events.NetStatusEvent;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;

import zdc.Util;

/**
 * ...
 * @author Simple Slime
 */
public class SoftVideo extends Sprite {
    private var vid:Video;
    private var ns:NetStream;
    private var nc:NetConnection;
    private var filePath:String;
    private var wid:Number;
    private var hei:Number;
    private var _playing:Boolean;
    public static const VIDEO_PLAY_END:String = "VIDEO_PLAY_END";
    public static const VIDEO_PLAY_START:String = "VIDEO_PLAY_START";

    /**
     *
     * @param    wid
     * @param    hei
     * @param    path
     * @param    immedatly
     */
    public function SoftVideo(wid:Number, hei:Number, path:String = "", immedatly:Boolean = false) {
        filePath = path;
        this.wid = wid;
        this.hei = hei;
        vid = new Video(wid, hei);
        this.addChild(vid);

        nc = new NetConnection();
        nc.connect(null);
        ns = new NetStream(nc);
        ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, onAsynErr);
        ns.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
        ns.client = this;
        vid.attachNetStream(ns);

        if (immedatly) {
            play(path);
        }
    }

    protected function onNetStatus(e:NetStatusEvent):void {
        switch (e.info.code) {
            case "NetStream.Play.Stop":
                dispatchEvent(new Event(VIDEO_ADAPTER_PLAY_END));
                _playing = false;
                break;
            case "NetStream.Play.Start":
                dispatchEvent(new Event(VIDEO_ADAPTER_PLAY_START));
                _playing = true;
                break;
        }
    }

    public function play(path:String = ""):void {
        if (path == "" && filePath == "") {
            return;
        }

        filePath = path == "" ? path : filePath;
        ns.play(path);
        _playing = true;
    }

    public function pause():void {
        ns.pause();
    }

    public function stop():void {
        ns.pause();
        ns.seek(0);
    }

    public function dispose():void {
        if (nc) {
            nc.close();
            nc = null;
        }
        if (ns) {
            ns.pause();
            ns.close();
            ns.removeEventListener(AsyncErrorEvent.ASYNC_ERROR, onAsynErr);
            ns.removeEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
            ns = null;
        }

        else if (vid) {
            vid.attachNetStream(null);
            Util.rmChild(vid);
            vid = null;
        }
    }

    private function onAsynErr(e:AsyncErrorEvent):void {

    }

    public function onMetaData(evt:Object):void {

    }

    public function onXMPData(info:Object):void {

    }

    public function onPlayStatus(info:Object):void {
        //trace("onPlayStatus", info.code);
    }

    public function get playing():Boolean {
        return _playing;
    }
}

}